package com.example.kliven.because.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.Singleton.GsonRequest;
import com.example.kliven.because.Singleton.StringRequestUtf;
import com.example.kliven.because.Singleton.VolleySingleton;
import com.example.kliven.because.adapter.MostTopicAdapter;
import com.example.kliven.because.bean.MostTopicBean;
import com.example.kliven.because.fragment.DiscoverFragment;
import com.example.kliven.because.intentbean.NetMostTopicBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MostTopicActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    private ListView listView;
    private List<MostTopicBean> mostTopicBeans;
    private MostTopicAdapter mostTopicAdapter;
    private RequestQueue queue;
    private String url = "http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_topic_tag_list&parameters=%7B%22_member_id%22:%220%22,%22_start%22:%220%22,%22_end%22:%2215%22%7D&memberId=0";
    private SwipeRefreshLayout swipeRefreshLayout;
    private Button btn_back_discover;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_most_topic);

        listView = (ListView) findViewById(R.id.list_view);
        btn_back_discover = (Button) findViewById(R.id.btn_back_discover);
        btn_back_discover.setOnClickListener(this);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        getNetData();
    }

    private void getNetData() {
        queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                mostTopicBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetMostTopicBean netMostTopicBean = gson.fromJson(response, NetMostTopicBean.class);
                for (int i = 0; i < netMostTopicBean.getData().getApp_topic_tag_list().size(); i++) {
                    MostTopicBean mostTopicBean = new MostTopicBean();
                    mostTopicBean.setImage_url(netMostTopicBean.getData().getApp_topic_tag_list().get(i).getImage_url());
                    mostTopicBean.setMember_share_tag_id(netMostTopicBean.getData().getApp_topic_tag_list().get(i).getMember_share_tag_id());
                    mostTopicBean.setShare_num(netMostTopicBean.getData().getApp_topic_tag_list().get(i).getShare_num());
                    mostTopicBean.setShare_tag_name(netMostTopicBean.getData().getApp_topic_tag_list().get(i).getShare_tag_name());
                    mostTopicBeans.add(mostTopicBean);
                }
                mostTopicAdapter = new MostTopicAdapter(mostTopicBeans, MostTopicActivity.this);
                listView.setAdapter(mostTopicAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                getNetData();
            }
        },5000);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MostTopicActivity.this, DiscoverFragment.class);
        startActivity(intent);
    }
}
